﻿using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// SpecificVersionWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SpecificVersionWindow : Window
    {
        public SpecificVersionWindow()
        {
            InitializeComponent();

            this.Loaded += SpecificVersionWindow_Loaded;
            this.Closing += SpecificVersionWindow_Closing;
        }

        void SpecificVersionWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NCLEventBus.Unregister(this);
        }

        private void SpecificVersionWindow_Loaded(object sender, RoutedEventArgs e)
        {
            NCLEventBus.Register(this);
            NCLEventBus.Publish(new BeginGetItemHistoryListEvent(UIContext.Current.CurrentSelectedNode.ID));
        }

        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            if(_ctlFileListView.SelectedItem != null)
            {
                ItemHistory history = _ctlFileListView.SelectedItem as ItemHistory;
                GetSpecificVersionThread.Start(UIContext.Current.CurrentSolution.ID, history.Version, history.ItemId, Progress, Completed);
            }
        }

        private void Completed(bool HasError, int successed, int total)
        {
            if (HasError)
            {
                this.Dispatcher.Invoke(() =>
                {
                    CustomMessageBox.ShowError("获取指定版本失败！");
                });
                return;
            }

            this.Dispatcher.Invoke(() =>
            {
                SolutionTreeViewNode selectedNode = UIContext.Current.CurrentSelectedNode;
                OpenSolutionThread.Start(UIContext.Current.CurrentSolution.ID);

                this.DialogResult = true;
            });
        }

        private void Progress(Guid id, string name, int current, int total)
        {
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void txtDateTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            for (int i = 0; i < _ctlFileListView.Items.Count; i++)
            {
                ItemHistory history = _ctlFileListView.Items[i] as ItemHistory;
                if (!txtDateTime.SelectedDate.HasValue)
                {
                    if (Convert.ToDateTime(history.OperatedTime).Date <= txtEndTime.SelectedDate.Value.Date)
                    {
                        ListViewItem item = _ctlFileListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
                        item.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        ListViewItem item = _ctlFileListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
                        item.Visibility = Visibility.Collapsed;
                    }
                }
                else if (!txtEndTime.SelectedDate.HasValue)
                {
                    if (Convert.ToDateTime(history.OperatedTime).Date >= txtDateTime.SelectedDate.Value.Date)
                    {
                        ListViewItem item = _ctlFileListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
                        item.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        ListViewItem item = _ctlFileListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
                        item.Visibility = Visibility.Collapsed;
                    }
                }
                else
                {
                    if (Convert.ToDateTime(history.OperatedTime).Date >= txtDateTime.SelectedDate.Value.Date
                        && Convert.ToDateTime(history.OperatedTime).Date <= txtEndTime.SelectedDate.Value.Date)
                    {
                        ListViewItem item = _ctlFileListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
                        item.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        ListViewItem item = _ctlFileListView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
                        item.Visibility = Visibility.Collapsed;
                    }
                }
            }
        }
    }
}
